package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.dao.impl.CompanyImpl;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.service.CompanyService;
import com.cn.wanxi.util.Tool;
import redis.clients.jedis.Jedis;

import java.util.List;

public class CompanyServiceImpl implements CompanyService {
    @Override
    public String add(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyImpl();
        int count = companyDao.add(companyModel);
        if (count > 0) {
            Jedis jedis = new Jedis();
            jedis.del("companyid");
        }
        return count > 0 ? "success" : "error";
    }

    @Override
    public String update(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyImpl();
        int count = companyDao.update(companyModel);
        if (count > 0) {
            Jedis jedis = new Jedis();
            jedis.del("companyid");
        }
        return count > 0 ? "success" : "error";
    }

    @Override
    public String delete(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyImpl();
        int count = companyDao.delete(companyModel);
        if (count > 0) {
            Jedis jedis = new Jedis();
            jedis.del("companyid");
        }
        return count > 0 ? "success" : "error";
    }

    @Override
    public CompanyModel findById(CompanyModel companyModel) {
        return null;
    }

    @Override
    public List<CompanyModel> findAll(CompanyModel companyModel) {
        return null;
    }

    /**
     * 1.以前所有数据来源于数据库
     * 2.当有大量访客访问时，不能查询数据库，需要查询redis
     * 3.首先要查询redis里面是否有数据，
     * 4.如果没有，则查询数据库，然后将数据库的值存入redis
     * 5.如果有，则直接查询redis
     *
     * @return
     */
    @Override
    public CompanyModel getCompanyModel() {
        CompanyModel companyModel = new CompanyModel();
        Jedis jedis = new Jedis();
        boolean isHave = jedis.exists("companyid");
        if (isHave) {
//去reids里面查询数据
//            List<String> lrange = jedis.lrange("companymodel", 0, jedis.llen("companymodel"));
            companyModel.setId(Tool.stringToInt(jedis.get("companyid")));
            companyModel.setLogo(jedis.get("logo"));
            companyModel.setEmail(jedis.get("email"));
//            System.out.println("redis");
//            companyModel.setLogo(jedis.get("logo"));
//            companyModel.setLogo(jedis.get("logo"));
//            companyModel.setLogo(jedis.get("logo"));
        } else {
//去数据库查询数据
            CompanyDao companyDao = new CompanyImpl();
            companyModel = companyDao.getCompanyModel();
//            System.out.println("mysql");
//            将数据存入redis
            jedis.set("companyid", companyModel.getId() + "");
            jedis.set("phone", companyModel.getPhone());
//            jedis.set("enable", companyModel.getEnable() + "");
            jedis.set("email", companyModel.getEmail());
            jedis.set("logo", companyModel.getLogo());
//            jedis.rpush("companymodel", companyModel.getAddress(), companyModel.getEmail())
        }
        return companyModel;
    }

}
